<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\common\AdminController;
use app\admin\model\Menu as MenuDB;
use think\console\command\make\Validate;
use think\Request;

class Menu extends AdminController
{
    protected $field = [
        "id",
        "component",
        "icon",
        "is_cache",
        "is_frame",
        "menu_name",
        "menu_type",
        "order_num",
        "path",
        "perms",
        "parent_id",
        "remark",
        "route_name",
        "status",
        "create_time"
    ];

    protected $allowField = [
        "component",
        "icon",
        "is_cache",
        "is_frame",
        "menu_name",
        "menu_type",
        "order_num",
        "path",
        "perms",
        "parent_id",
        "remark",
        "route_name",
        "status",
    ];

    protected function initialize()
    {
        $this->model = new MenuDB();
    }

    public function list(array $rule = [], array $field = [], array $association = [], array $hidden = [])
    {
        // $list = $this->model->select()->toArray();
        $res = parent::list();
        $dataRes = $res instanceof \think\response\Json ? $res->getData() : [];

        if ($dataRes['code'] == 200) {
            $data = buildTree($dataRes['data']['rows']->toArray());
            return success("获取成功", ['rows' => $data]);
        }
        return $res;
    }

    public function add(array $allowField = [], array $association = [], $data = [], $returnId = false)
    {
        $datas = input('post.');
        if (empty($datas['menuName'])) {
            return error("菜单名称不能为空");
        }
        $allowField = $this->allowField;
        $allowField[] = 'create_time';
        $allowField[] = 'create_by';
        return parent::add($allowField);
    }

    public function update(array $allowField = [], array $where = [], array $association = [], array $data = [])
    {
        $datas = input('post.');
        if (empty($datas['menuName'])) {
            return error("菜单名称不能为空");
        }
        $allowField = $this->allowField;
        $allowField[] = 'update_time';
        $allowField[] = 'update_by';
        return parent::update($allowField);
    }

    public function get($id, array $field = [], array $where = [], array $association = [], array $hidden = [])
    {

        return parent::get($id, $this->field);
    }
    public function del($ids, $type = false, $where = [])
    {
        if ($this->model->where('parent_id', $ids)->count() > 0) {
            return error("当前菜单下有子菜单，请先删除子菜单！");
        }
        return parent::del($ids);
    }

    public function menuRoleTree($id)
    {
        // $menuList = $this->model
        //     ->field('m.id, m.parent_id, m.menu_name as label, m.id as value, 
        //      IF(rm.role_id IS NOT NULL, 1, 0) as checked')
        //     ->alias('m')
        //     ->leftJoin('role_menu rm', 'm.id = rm.menu_id AND rm.role_id = ' . $id)
        //     ->select()
        //     ->toArray();
        $check = [];
        if (!empty($id))
            $check = \app\admin\model\RoleMenu::where('role_id', $id)->column('menu_id');
        $menu = $this->model
            ->field('id,menu_name as label,parent_id,status as disabled')
            ->withAttr('disabled', function ($val) {
                return $val == 1 ? false : true;
            })->select()->toArray();
        $menu = buildTree($menu);
        return success("获取成功", ['rows' => $menu, 'check' => $check]);
    }


}
